﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DailyComics.Common.Model.ImageProvideStrategies;
using DailyComics.Common.Model.CachingStrategies;

namespace DailyComics.Common.Model
{
  public class CachingImageProvider : ImageProvider
  {
    private ICachingStrategy cachingStrategy;

    public CachingImageProvider(IImageProviderStrategy providerStrategy, ICachingStrategy cachingStrategy)
      : base(providerStrategy)
    {
      this.cachingStrategy = cachingStrategy;
    }

    public override byte[] GetImage(DateTime date)
    {
      if (cachingStrategy.Contains(date)) return cachingStrategy.Get(date);

      var image = providerStrategy.GetImage(date);

      cachingStrategy.Add(date, image);

      return image;
    }
  }
}
